<template>
	<view class="wrap">
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
			</view>
		</view>
		<view class="activity-list">
			<view class="b-items" v-if="shopList.length > 0">
				<view class="b-item" v-for="(item, index) in shopList" :key="index">
					<view class="b-cover" v-if="item.cover_url">
						<image :src="item.cover_url" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">
							<view class="s-name">{{item.name}}</view>
						</view>
						<view class="b-label" v-if="item.start_time && item.end_time">
							<view class="s-title">营业时间：</view>
							<view class="s-cont">{{item.start_time}} - {{item.end_time}}</view>
						</view>
						<view class="b-label" v-if="item.contact">
							<view class="s-title">联系方式：</view>
							<view class="s-cont" @click="onPhoneCall(item.contact)">{{item.contact}}</view>
						</view>
						<view class="b-label f-address" v-if="item.address">
							<view class="s-title">所在地址：</view>
							<view class="s-cont" @click="onOpenAddress(index)">
								<view>{{item.address}}</view>
								<view class="iconfont icon-direct" v-if="item.lat && item.lng"></view>
							</view>
						</view>
						<view class="b-foot">
							<view class="b-state">
								<view class="s-state f-rest" v-if="item.state == 0">已休息</view>
								<view class="s-state f-ing" v-else-if="item.state == 1">营业中</view>
							</view>
							<view class="b-action">
								<view class="s-enter" @click="onChoose(item.id)">进入点餐</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
	</view>
</template>

<script>
	import {
		getShopList
	} from '@/service/api/shop.js'
	import {
		formatDate
	} from '@/common/util.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				groupIndex: 0,
				groupList: [{
						name: '全部',
						value: 'all',
					},
					{
						name: '营业中',
						value: 'ing',
					},
					{
						name: '已休息',
						value: 'rest',
					},
				],
				shopList: [],
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadShopList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadShopList();
			}
		},
		computed: {
			datetime() {
				return (timestamp) => {
					return formatDate(timestamp, 'yyyy-MM-dd hh:mm')
				}
			}
		},
		methods: {
			loadShopList(refresh) {
				console.log('loadShopList', refresh);
				if (refresh) {
					this.page = 1
					this.shopList = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				const group = this.groupList[this.groupIndex].value
				getShopList(group, this.page, 10).then(([err, res]) => {
					console.log('getShopList', err, res)
					this.loadingType = -1
					if (!err) {
						if (res.items.length > 0) {
							this.shopList = [...this.shopList, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onGroupSelect(index) {
				this.groupIndex = index
				this.loadShopList(true)
			},
			onChoose(id) {
				console.log('onChoose', id)
				this.$store.commit('setShopId', id)
				this.$store.commit('delDeskId')
				uni.reLaunch({
					url: '/pages/home/index?from=jump'
				})
			},
			onPhoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			onOpenAddress(index) {
				const shopData = this.shopList[index]
				if (!shopData.lat || !shopData.lng) {
					return
				}
				uni.openLocation({
					latitude: shopData.lat,
					longitude: shopData.lng,
					name: shopData.name,
					address: shopData.address
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}

	.filter-box {
		padding: 30upx;
		background: #fff;
		display: flex;
		align-items: center;

		.b-item {
			width: 240upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;

			&.f-active {
				font-weight: bold;
			}

			.s-tit {
				color: #333;
			}
		}
	}

	.activity-list {
		padding: 40upx 40upx 0 40upx;

		.b-items {
			.b-item {
				margin-bottom: 40upx;
				border-bottom: 1upx solid #eee;
				display: flex;
				flex-direction: column;
				background: #fff;
				border-radius: 20upx;

				&:last-child {
					border-bottom: none;
					margin-bottom: 0;
				}

				.b-cover {
					width: 670upx;
					height: 300upx;

					image {
						border-radius: 20upx 20upx 0 0;
						width: 670upx;
						height: 300upx;
					}
				}

				.b-main {
					padding: 20upx;
				}

				.b-title {
					display: flex;
					align-items: center;

					.s-name {
						font-size: 30upx;
						color: #333;
					}
				}

				.b-label {
					margin-top: 10upx;
					display: flex;
					align-items: center;
					font-size: 26upx;
					color: #999;

					&.f-address {
						.s-cont {
							display: flex;
							align-items: center;
						}

						.iconfont {
							margin-left: 10upx;
							font-size: 36upx;
							color: #aaa;
						}
					}
				}

				.b-foot {
					margin-top: 10upx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.b-state {
						.s-state {
							padding: 0 20upx;
							background: #DA5650;
							color: #fff;
							font-size: 24upx;
							height: 40upx;
							line-height: 40upx;

							&.f-rest {
								background: #444c69;
							}

							&.f-ing {
								background: #FFC300;
							}
						}

					}

					.b-action {
						.s-enter {
							padding: 0 30upx;
							height: 60upx;
							line-height: 60upx;
							text-align: center;
							color: #fff;
							font-size: 28upx;
							background: #DA5650;
							border-radius: 30upx;
						}
					}
				}
			}
		}
	}
</style>
